package com.hzsd.zengbanyi.biaodan_text.fragment;

import java.util.ArrayList;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.hzsd.zengbanyi.biaodan_text.Actrivity.DetailListActivity;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.MoreBean;
import com.hzsd.zengbanyi.biaodan_text.bean.WorkDAO;
import com.hzsd.zengbanyi.biaodan_text.events.TaskSceneEvent;

/**
 * 污染源--相关场景页
 * @author Administrator
 *
 */
public class PollutionSceneFragment extends Fragment {
	private View view;
	private LinearLayout ll_HourDay;
	private TextView textName, textDate, textEnvironment,
			tv_FixingAgent, tv_ways, tv_num, tv_ProductionLoad, tv_Operation,
			tv_MethodsAndSources, MethodsAndSources;
	private EditText ed_Temperature, ed_Disruptor, ed_ProductionLoad,
			ed_DeviceName, ed_Remarks,et_HourDay,et_StandardizationSituation;
	private WorkDAO taskDAO;
	private Spinner ways, num, sp_weather, sp_Operation;
	private View view_ways, view_num, view_ProductionLoad, view_Operation,
			view_HourDay, view_MethodsAndSources;
	private MoreBean moreBean;

	private String title, taskId, taskSceneId, sceneStartTime, taskways,
			tasknum, weather, Temperature, sys_auto_time;

	private String Fixing;
	private String type = "污染源废水";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initBaseData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = LayoutInflater.from(getActivity()).inflate(
				R.layout.fragment_pollutionscene, container, false);
		initView(view);
		initEvents();
		return view;
	}

	private void initBaseData() {
		taskDAO = new WorkDAO(getActivity());
		
		SharedPreferences share = getActivity().getSharedPreferences("user",
				getActivity().MODE_PRIVATE);
		title = share.getString("title", "");
		taskId = share.getString("taskId", "");
		taskSceneId = share.getString("taskSceneId", "");
		sceneStartTime = share.getString("sceneStartTime", "");

	}

	private void initView(View view) {
		textName = (TextView) view.findViewById(R.id.taskname);
		textDate = (TextView) view.findViewById(R.id.taskdate);
		textEnvironment = (TextView) view.findViewById(R.id.fieldenvironment);
		tv_FixingAgent = (TextView) view.findViewById(R.id.tv_FixingAgent);
		ed_Disruptor = (EditText) view.findViewById(R.id.ed_Disruptor);
		ed_ProductionLoad = (EditText) view
				.findViewById(R.id.ed_ProductionLoad);
		ed_DeviceName = (EditText) view.findViewById(R.id.ed_DeviceName);
		sp_Operation = (Spinner) view.findViewById(R.id.sp_Operation);
		ed_Remarks = (EditText) view.findViewById(R.id.ed_Remarks);
		view_ways = view.findViewById(R.id.view_ways);
		view_num = view.findViewById(R.id.view_num);
		tv_ways = (TextView) view.findViewById(R.id.tv_ways);
		tv_num = (TextView) view.findViewById(R.id.tv_num);
		view_ProductionLoad = view.findViewById(R.id.view_ProductionLoad);
		view_Operation = view.findViewById(R.id.view_Operation);
		tv_ProductionLoad = (TextView) view
				.findViewById(R.id.tv_ProductionLoad);
		tv_Operation = (TextView) view.findViewById(R.id.tv_Operation);
		ll_HourDay = (LinearLayout) view.findViewById(R.id.ll_HourDay);
		view_HourDay = view.findViewById(R.id.view_HourDay);
		view_MethodsAndSources = view.findViewById(R.id.view_MethodsAndSources);
		tv_MethodsAndSources = (TextView) view
				.findViewById(R.id.tv_MethodsAndSources);
		MethodsAndSources = (TextView) view
				.findViewById(R.id.MethodsAndSources);
		ways = (Spinner) view.findViewById(R.id.ways);
		num = (Spinner) view.findViewById(R.id.num);
		// 天气，气温
		sp_weather = (Spinner) view.findViewById(R.id.sp_weather);
		ed_Temperature = (EditText) view.findViewById(R.id.ed_Temperature);
		et_HourDay = (EditText) view.findViewById(R.id.et_HourDay);
		et_StandardizationSituation = (EditText) view.findViewById(R.id.et_StandardizationSituation);
	}

	private void initEvents() {
		textName.setText(title);
		textDate.setText(sceneStartTime);
		moreBean = new MoreBean();
		//初始化公共的控件事件
		TaskSceneEvent taskSceneEvent = new TaskSceneEvent(getActivity());
		//小时/每日
		taskSceneEvent.editTextSaveData(et_HourDay, taskSceneId, "小时/每日", type);
		taskSceneEvent.editTextSaveData(ed_Disruptor, taskSceneId, "样品可能含有的干扰物", type);
		taskSceneEvent.editTextSaveData(ed_DeviceName, taskSceneId, "现场监测仪器设备名称、型号及编号", type);
		taskSceneEvent.editTextSaveData(ed_ProductionLoad, taskSceneId, "生产负荷", type);
		taskSceneEvent.editTextSaveData(ed_Remarks, taskSceneId, "备注", type);
		taskSceneEvent.editTextSaveData(et_StandardizationSituation, taskSceneId, "规范化情况", type);
		//气温
		taskSceneEvent.editTextSaveData(ed_Temperature, taskSceneId, "气温", type);
		//固定剂加入情况
		taskSceneEvent.fixingAgent(tv_FixingAgent,taskSceneId, "固定剂加入情况", type);
		//采样和分析方法及来源
		taskSceneEvent.fixingAgent(MethodsAndSources, taskSceneId, "采样和分析方法及来源", type);
		
		sp_weather.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				weather = parent.getItemAtPosition(position).toString();
				moreBean.taskID = taskId;
				moreBean.scene_weather = weather;
				taskDAO.updateMoreWeather(moreBean);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		ways.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				taskways = parent.getItemAtPosition(position).toString();
				moreBean.taskID = taskId;
				moreBean.taskways = taskways;
				moreBean.tasknum = tasknum;
				moreBean.taskSceneId = taskSceneId;
				moreBean.sys_auto_time = sys_auto_time;
				moreBean.createtime = ((DetailListActivity) getActivity()).getCurrentTime();
				taskDAO.updateMoreWays(moreBean);
				taskDAO.addSceneMore(moreBean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		num.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				tasknum = parent.getItemAtPosition(position).toString();
				moreBean.taskID = taskId;
				moreBean.taskways = taskways;
				moreBean.tasknum = tasknum;
				moreBean.taskSceneId = taskSceneId;
				moreBean.sys_auto_time = sys_auto_time;
				moreBean.createtime = ((DetailListActivity) getActivity()).getCurrentTime();
				taskDAO.updateMoreNum(moreBean);
				taskDAO.addSceneMore(moreBean);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		MoreBean moreBean2 = taskDAO.getMoreBean(taskId);
		taskways = moreBean2.taskways;
		tasknum = moreBean2.tasknum;
		sys_auto_time = moreBean2.sys_auto_time;
		System.out.println("数据：" + moreBean2.taskways);
		switch (moreBean2.taskways) {
		case "混合":
			ways.setSelection(0);
			break;
		case "连续":
			ways.setSelection(1);
			break;
		case "间歇":
			ways.setSelection(2);
			break;
		case "瞬时":
			ways.setSelection(3);
			break;

		default:
			ways.setSelection(0);
			break;
		}
		switch (moreBean2.tasknum) {
		case "连续":
			num.setSelection(0);
			break;
		case "间歇":
			num.setSelection(1);
			break;

		default:
			num.setSelection(0);
			break;
		}
		// 进入页面，立即查询出原有天气，显示出来
		String weather = moreBean2.scene_weather;
		if ("".equals(weather)) {
			moreBean2.scene_weather = "晴";
		}
		switch (moreBean2.scene_weather) {
		case "阴":
			sp_weather.setSelection(0);
			break;
		case "晴":
			sp_weather.setSelection(1);
			break;

		default:
			sp_weather.setSelection(1);
			break;
		}
		
		// 查询出设备运转情况
		switch (taskDAO.getTaskSceneMoreField(taskSceneId,"设施运转情况",type)) {
		case "正常":
			sp_Operation.setSelection(1);
			break;
		case "不正常":
			sp_Operation.setSelection(2);
			break;

		default:
			sp_Operation.setSelection(0);
			break;
		}

		sp_Operation.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {

				taskDAO.updateTaskSceneMoreField(taskSceneId, parent
						.getItemAtPosition(position).toString(),
						"设施运转情况",type);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

	

	}

}
